import com.sun.xml.internal.bind.v2.model.core.ID;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import sun.awt.geom.AreaOp.EOWindOp;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/11 9:07
 * @description:接口实现类
 */
public class UserDaoIO implements UserDao {

  @Override
  public void register(User user) {
    BufferedWriter bw=null;
    try {
      bw=new BufferedWriter(new FileWriter("D:\\user.txt"));
      bw.write(user.getId()+"--"+user.getName()+"--"+user.getPassword()+"--"+user.getAge());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {

      System.out.println("出现异常"+e.getMessage());
    }finally {
      if (bw!=null){
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public boolean login(String name, String password) {
    BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("D:\\user.txt"));
      String line;
      while ((line=br.readLine())!=null){
        String[] data=line.split("--");
        //比较
        if (data[1].equals(name)&&data[2].equals(password)){
          return true;
        }
      }
    } catch (Exception e) {
      System.out.println("出现异常"+e.getMessage());
    }finally {
      if (br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return false;
  }

  @Override
  public void queryAll() {
    BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("D:\\user.txt"));
      String line=null;
      while ((line=br.readLine())!=null){
        System.out.println(line);
      }
    } catch (Exception e) {
      System.out.println("出现异常"+e.getMessage());
    }finally {
      if (br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

/*
  @Override
  public void register(User user) {
    BufferedWriter bw=null;
    try {
      bw=new BufferedWriter(new FileWriter("D:\\user.txt",true));
      bw.write(user.getId()+"--"+user.getName()+"--"+user.getPassword()+"--"+user.getAge());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(bw!=null){
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public boolean login(String name, String password) {
    BufferedReader br=null;
    try {
      br=new BufferedReader(new FileReader("D:\\user.txt"));
      String line=null;
      while ((line=br.readLine())!=null){
        String data[]=line.split("--");
        if(data[1].equals(name)&&data[2].equals(password)){
          return true;
        }
      }
    } catch (Exception e) {
      System.out.println("出现异常"+e.getMessage());
    }finally {
      if (br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

    }
    return false;
  }

  @Override
  public void queryAll() {
    BufferedReader br=null;
    try {
      br = new BufferedReader(new FileReader("D:\\user.txt"));
      String line=null;
      while ((line=br.readLine())!=null){
        System.out.println(line);
      }
    } catch (Exception e) {
      System.out.println("出现异常"+e.getMessage());
    }finally {
      if (br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }*/

}
